package dao;

import entity.Department;
import entity.Teacher;
import exception.BysjException;
import service.DepartmentService;
import util.Condition;
import util.JdbcHelper;
import util.Pagination;

import java.sql.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;

public class TeacherDao implements TeacherDaoInterface{
    private static TeacherDao teacherDao = null;
    private TeacherDao(){

    }
    public static TeacherDao getInstance(){
        if(teacherDao==null){
            teacherDao=new TeacherDao();
        }
        return teacherDao;
    }

    @Override
    public Collection<Teacher> findAll(Connection connection, List<Condition> conditionLsit) throws SQLException, BysjException {
        Collection<Teacher> desiredTeachers=new HashSet<Teacher>();
        StringBuilder sql=new StringBuilder("SELECT * FROM teacher");
        if(conditionLsit!=null){
            String whereClause=Condition.toWhereClause(conditionLsit);
            sql.append(whereClause);
            System.out.println(sql.toString());
        }
        int totalNum=CommonDao.getInstance().count(connection,"Teacher");
        PreparedStatement ps=connection.prepareStatement(sql.toString());
        ResultSet rs=ps.executeQuery();
        while(rs.next()){
            Long id=rs.getLong("id");
            String name=rs.getString("name");
            Long departmentId=rs.getLong("department_id");
            Department department= DepartmentService.getInstance().find(departmentId);
            Teacher desiredTeacher=new Teacher(id,name,department);
            System.out.println(desiredTeacher.getDepartment().getDescription());
            desiredTeachers.add(desiredTeacher);
        }
        JdbcHelper.close(rs,ps,connection);
        return desiredTeachers;
    }

    @Override
    public Collection<Teacher> findAll
            (Connection connection, List<Condition> conditionList, Pagination pagination)
            throws SQLException, BysjException {
        Collection<Teacher> desiredTeachers=new HashSet<>();
        StringBuilder sql=new StringBuilder("SELECT * FROM teacher");
        if(conditionList!=null){
            String whereClause=Condition.toWhereClause(conditionList);
            sql.append(whereClause);
            System.out.println(sql.toString());
        }
        int totalNum=CommonDao.getInstance().count(connection,"Teacher",conditionList);
        String paginationClause=pagination.toLimitClause(totalNum);
        sql.append(paginationClause);
        System.out.println(sql.toString());
        PreparedStatement ps=connection.prepareStatement(sql.toString());
        ResultSet rs=ps.executeQuery();
        while(rs.next()){
            Long id=rs.getLong("id");
            String name=rs.getString("name");
            Long departmentId=rs.getLong("department_id");
            Department department= DepartmentService.getInstance().find(departmentId);
            Teacher desiredTeacher=new Teacher(id,name,department);
            System.out.println(desiredTeacher.getDepartment().getDescription());
            desiredTeachers.add(desiredTeacher);
        }
        JdbcHelper.close(rs,ps,connection);
        return desiredTeachers;
    }
}
